

import java.sql.*;

/**
 * A MySQL class that extends Database
 *
 */
public class SQLDatabase extends Database {
    public SQLDatabase(String url, String username, String password) {
        setUrl(url);
        setUsername(username);
        setPassword(password);
    }

    /**
     * Close connection, return true if succesful
     * 
     * @return boolean
     * @throws SQLException
     */
    public boolean close() {
        try {
            getConnection().close();
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    /**
     * Connect to database, return true if succesful
     * 
     * @return boolean
     * @throws SQLException
     */
    public boolean connect() {
        Connection connection;
        
        try{ 
            connection = DriverManager.getConnection(getUrl(), getUsername(), getPassword());
            setConnection(connection);
            System.out.println("Database connected");
            return true;
        } catch(SQLException e) {
            System.out.println("Database not connected");
            return false;
        }
    }

    /**
     * Execute SQL, return false if unsuccesful
     * 
     * @param sql
     * @return
     * @throws SQLException
     */
    public boolean executeSQL(String sql) {
        Statement statement;
        ResultSet rs;
        
        try {
            statement = getConnection().createStatement();
            rs = statement.executeQuery(sql);
            setResultSet(rs);
            
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    /**
     * Execute Update SQL, return false if unsuccesful
     * 
     * @param sql
     * @return
     * @throws SQLException
     */
    public boolean executeUpdateSQL(String sql) {
        Statement statement;
        
        try {
            statement = getConnection().createStatement();
            statement.executeUpdate(sql);
            System.out.println("SQL Update Executed, success");
            
            return true;
        } catch (SQLException e) {
            System.out.println("SQL Update Executed, failed");
            return false;
        }
    }
  
    /**
     * Return true if connected to DB-server
     * 
     * @return boolean
     * @throws SQLException
     */
    public boolean isConnected() {
        try {
            if(getConnection().isClosed()) {
                return false;
            } else {
                return true;
            }
        } catch (SQLException e) {
            return false;
        }
    }
  
    /**
     * Load driver, return true if succesful
     * 
     * @return boolean
     * @throws ClassNotFoundException
     */
    public boolean loadDriver() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Driver loaded");
            return true;
        } catch (ClassNotFoundException e) {
            System.out.println("Driver not loaded");
            return false;
        }
    }
}